﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Configuration;

namespace SharePoint2007Migration
{
    public static class ConfigHelper
    {
        #region Configuration File Entries

        private const string CNF_SOURCE_REPORT_PATH = "SourceReportFilePath";
        private const string CNF_SOURCE_REPORT_FILE = "SourceReportFileName";
        private const string CNF_DEST_REPORT_PATH = "DestinationReportFilePath";
        private const string CNF_APP_LOG_PATH = "ApplicationLogFilePath";
        private const string CNF_APP_LOG_LEVEL = "ApplicationLogVerboseLevel";
        private const string CNF_QUERY_GET_MISSING_WP_INFO = "QueryToGetMissingWPInfo";

        #endregion

        #region Public Attributes

        public static readonly string SourceReportFilePath;
        public static readonly string SourceReportFileName;
        public static readonly string DestintationReportFilePath; 
        public static readonly LogVerboseLevel VerboseLevel;
        public static readonly string LogFilePath;
        public static readonly string SourceConnectionString;
        public static readonly string QueryToGetMissingWpInfo;

        #endregion

        static ConfigHelper()
        {
            try
            {
                string appPath = Environment.CurrentDirectory;
                //string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                AppSettingsReader appSettingReader = new System.Configuration.AppSettingsReader();

                SourceReportFilePath = (string)appSettingReader.GetValue(CNF_SOURCE_REPORT_PATH, typeof(string));
                if (SourceReportFilePath == string.Empty) SourceReportFilePath = appPath;

                SourceReportFileName = (string)appSettingReader.GetValue(CNF_SOURCE_REPORT_FILE, typeof(string));

                DestintationReportFilePath = (string)appSettingReader.GetValue(CNF_DEST_REPORT_PATH, typeof(string));
                if (DestintationReportFilePath == string.Empty) DestintationReportFilePath = appPath;

                LogFilePath = (string)appSettingReader.GetValue(CNF_APP_LOG_PATH, typeof(string));
                if (LogFilePath == string.Empty) LogFilePath = appPath;

                string confVerboseLevel = (string)appSettingReader.GetValue(CNF_APP_LOG_LEVEL, typeof(string));
                if (confVerboseLevel == string.Empty) VerboseLevel = LogVerboseLevel.Low;
                else VerboseLevel = (LogVerboseLevel)int.Parse(confVerboseLevel);

                QueryToGetMissingWpInfo = (string)appSettingReader.GetValue(CNF_QUERY_GET_MISSING_WP_INFO, typeof(string));
                if (string.IsNullOrEmpty(QueryToGetMissingWpInfo))
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendLine("SELECT");
                    stringBuilder.AppendLine("SiteId, AllDocs.DirName as SiteUrl, AllDocs.LeafName as PageUrl, AllDocs.DirName + '/' + AllDocs.LeafName as Url,");
                    stringBuilder.AppendLine("WebParts.tp_DisplayName as DisplayName");
                    stringBuilder.AppendLine("FROM");
                    stringBuilder.AppendLine("[dbo].[AllDocs]");                 
                    stringBuilder.AppendLine("INNER JOIN");
                    stringBuilder.AppendLine("[dbo].[WebParts]");                 
                    stringBuilder.AppendLine("ON");
                    stringBuilder.AppendLine("[AllDocs].Id = [WebParts].[tp_PageUrlID]");
                    stringBuilder.AppendLine("WHERE");
                    stringBuilder.AppendLine("[WebParts].[tp_WebPartTypeId] = '{0}'  ORDER BY Url"); // I.e.: {0}=7586a7b2-b35c-da44-fe31-5d5574e5b4b7
                    
                    QueryToGetMissingWpInfo = stringBuilder.ToString(); 
                }
            }
            catch (Exception exc)
            {
                LogHelper.TraceError(exc);
                throw new Exception("An exception has occurred while reading App.Config file: check your configuration!");
            }
            finally
            {   
            }
        }
    }
}
